
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author sa-trainee
 */
public class ServletDianostic extends HttpServlet implements Servlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        Map requestMap = this.getQueryMap(request.getQueryString());
        String action = "" + requestMap.get("action");
        PrintWriter out = response.getWriter();
        if (action != null && action.equals("data")) {
            String datasent = "" + requestMap.get("dataSent");
            try {

                if (datasent != null || (!datasent.equals(""))) {

                    byte[] dataBytes = datasent.getBytes();
                    byte[] apacheBytes = org.apache.commons.codec.binary.Base64.decodeBase64(dataBytes);
                    String data = new String(apacheBytes);
                    File file = new File("dataSent.txt");
                    if (!file.exists()) {  // this will return boolean {true} if file exists.
                        file.createNewFile(); // create new empty file.
                    }
                    BufferedWriter fileOut = new BufferedWriter(new FileWriter(file, true));
                    fileOut.newLine();
                    fileOut.write(data);
                    fileOut.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            out.println("<html>");
            out.println("<body>");
            out.println("<h1>OK</h1>");
            out.println("</body>");
            out.println("</html>");
        } else {
            out.println("<html>");
            out.println("<body>");
            out.println("<h1>Hello Servlet Get</h1>");
            out.println("</body>");
            out.println("</html>");
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String dataStr = request.getParameter("dataSent");       
        PrintWriter out = response.getWriter();
        if (action != null && action.equals("data")) {
            String datasent = "" + dataStr;
            try {

                if (datasent != null || (!datasent.equals(""))) {

                    byte[] dataBytes = datasent.getBytes();
                    byte[] apacheBytes = org.apache.commons.codec.binary.Base64.decodeBase64(dataBytes);
                    String data = new String(apacheBytes);
                    File file = new File("D:/dataSent.txt");
                    if (!file.exists()) {  // this will return boolean {true} if file exists.
                        file.createNewFile(); // create new empty file.
                    }
                    BufferedWriter fileOut = new BufferedWriter(new FileWriter(file, true));
                    fileOut.newLine();
                    fileOut.write(data);
                    fileOut.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            out.println("<html>");
            out.println("<body>");
            out.println("<h1>OK</h1>");
            out.println("</body>");
            out.println("</html>");
        } else {
            out.println("<html>");
            out.println("<body>");
            out.println("<h1>Hello Servlet Get</h1>");
            out.println("</body>");
            out.println("</html>");
        }
        out.flush();
        out.close();
    }
    private Map<String, String> getQueryMap(String query) {
        String[] params = query.split("&");
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < params.length; i++) {
            String param = params[i];           
            int idx = param.indexOf("=");
            String name = param.substring(0, idx);
            String value = param.substring(idx + 1);
            map.put(name, value);
        }
        return map;
    }

}
